/**
 * related to memberList.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-09-27
 */
var app = sm("do_App");
var page = sm("do_Page");
var hashdata = mm("do_HashData");

var helperOpen = require("helper/open");
var homemakingUtil = require("url/homemaking");
var qqHttp = require("helper/http");

var listdata = mm("do_ListData");
var listView_member = ui("listView_member");
listView_member.bindItems(listdata);

//andriod返回键
page.on("back", function() {
    app.closePage();
});
/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : '选择服务人员'
})
header.refreshData();

var param = page.getData();
page.on("loaded", function() {
	refreshAllData();
})

var pageNum = 1, totalPages = 1;
function refreshAllData(){
	pageNum = 1;
	param.page = pageNum;
	qqHttp.post(homemakingUtil.url.base.getCanServiceMemberList, param, function(data) {
		//deviceone.print('----refreshAllData----'+JSON.stringify(data));
		totalPages = data.page.totalPages;
		listView_member.rebound();
		listdata.removeAll();
		listdata.addData(data.page.content);
		listView_member.refreshItems();
	});
}

function getNextPageData(){
	pageNum++;
	if (pageNum <= totalPages) {
		param.page = pageNum;
		qqHttp.post(userUtil.url.base.getCanServiceMemberList, param, function(data) {
			//deviceone.print('----getNextPageData----'+JSON.stringify(data));
			totalPages = data.page.totalPages;
			listdata.addData(data.page.content);
			listView_member.rebound();
			listView_member.refreshItems();
		});
	} else {
		nf.toast("已是最后一页");
		listView_member.rebound();
		listView_member.refreshItems();
	}
}

//下拉列表，刷新数据
listView_member.on("pull", function(data){
	//其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){
		refreshAllData();
	}
});

//上拉列表，翻页数据
listView_member.on("push", function(data){
	//其中state=0：表示开始上推headerview，；state=1：表示上推headerview超过headerview的高度，触发一次这个事件；state=2：上推超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){	
		getNextPageData();
	}
});

//点击一条
/*listView_member.on("touch", function(data){
	var one = listdata.getOne(data);
	one.fromPage = "memberList";
	app.closePage(one);
});*/